package com.jinyi.wwdeng.ui.config.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import cn.bingoogolapple.baseadapter.BGAOnItemChildClickListener
import com.jinyi.base.base.BaseFragment
import com.jinyi.base.utils.HHLog
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentConfirmerBinding
import com.jinyi.wwdeng.ui.config.adapter.ConfirmBindCardAdapter
import com.jinyi.wwdeng.utils.dialog.BindCardDeleteDialog
import com.jinyi.wwdeng.utils.dialog.BindCardDialog
import com.jinyi.wwdeng.utils.dialog.BindCardReplaceDialog
import com.jinyi.wwdeng.utils.dialog.BindCardSubmitDialog
import com.jinyi.wwdeng.utils.dialog.BindCardSubmitWarningDialog
import com.jinyi.wwdeng.utils.recyclerview.LinearSpacingItemDecoration
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/18 17:45
 * @Annotation：确认人
 */
class ConfirmerFragment: BaseFragment() {

    lateinit var binding: FragmentConfirmerBinding
    //绑卡列表
    var mBindCardAdapter: ConfirmBindCardAdapter? = null
    //绑定弹框
    var mBindCardDialog: BindCardDialog? = null

    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentConfirmerBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
        //新增
        binding.btnAdd.setOnClickListener {
            showToast("新增")
        }
        //提交
        binding.btnSubmit.setOnClickListener {
            showSubmitWarningDialog()
        }
    }

    override fun initData() {
        val list = arrayListOf<JSONObject>()
        for (i in 0..3){
            val obj = JSONObject()
            if (i <= 1){
                obj.put("isEdit",false)
            } else {
                obj.put("isEdit",true)
            }
            list.add(obj)
        }
        mBindCardAdapter!!.data = list
    }

    /**
     * 初始化绑卡列表
     */
    private fun initRecyclerView() {
//        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        mBindCardAdapter = ConfirmBindCardAdapter(binding.recyclerView)
        val topDecoration = App.dp2px(14f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,topDecoration,topDecoration))
        binding.recyclerView.adapter = mBindCardAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        mBindCardAdapter!!.setOnItemChildClickListener(BGAOnItemChildClickListener { parent, childView, position ->
            when(childView.id){
                R.id.editLayout -> {//编辑
                    showBindCardDialog()
                }
                R.id.cardLayout -> {//查看已绑卡
                    showToast("查看")
                }
                R.id.btnDelete -> {//删除
                    showDeleteDialog(position)
                }
            }
        })
    }

    /**
     * 添加绑卡dialog
     */
    private fun showBindCardDialog(){
        if (mBindCardDialog == null){
            mBindCardDialog = BindCardDialog(context,object : BindCardDialog.IDialogCallback{
                override fun onClick(view: View?) {
                }
                override fun onSubmitParams(typeObj: JSONObject, phoneNumber: String) {
                    HHLog.e("确认绑定>>>>>>>>>>$typeObj>>>>>>>>>$phoneNumber")
                    if (phoneNumber == "123456"){
                        mBindCardDialog!!.showTypeErrorView()
                    } else {
                        mBindCardDialog!!.dismiss()
                        showBindCardReplaceDialog()
                    }
                }
            })
            mBindCardDialog!!.setCancelable(true)
            mBindCardDialog!!.setCanceledOnTouchOutside(false)
        }
        mBindCardDialog!!.show()
    }

    /**
     * 替换账户dialog
     */
    private fun showBindCardReplaceDialog(){
        val replaceDialog = BindCardReplaceDialog(context)
        replaceDialog.show()
    }

    /**
     * 警告提示
     */
    private fun showSubmitWarningDialog(){
        val warningDialog = BindCardSubmitWarningDialog(context,object : BindCardSubmitWarningDialog.IDialogCallback{
            override fun onClick(view: View?) {
                if (view!!.id == R.id.tvConfirm){
                    showConfirmDialog()
                }
            }
        })
        warningDialog.show()
    }

    /**
     * 删除提示
     */
    private fun showDeleteDialog(position: Int){
        val deleteDialog = BindCardDeleteDialog(context,object : BindCardDeleteDialog.IDialogCallback{
            override fun onClick(view: View?) {
                if (view!!.id == R.id.btnRight){
                    mBindCardAdapter!!.removeItem(position)
                }
            }
        })
        deleteDialog.show()
    }

    /**
     * 确认dialog
     */
    private fun showConfirmDialog(){
        val submitDialog = BindCardSubmitDialog(context,object : BindCardSubmitDialog.IDialogCallback{
            override fun onClick(view: View?) {
                if (view!!.id == R.id.btnConfirm){
                    showToast("确认提交")
                }
            }
        })
        submitDialog.show()
    }
}